define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {

            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'census/transaction/index' + location.search,
                    add_url: 'census/transaction/add',
                    // edit_url: 'census/transaction/edit',
                    // del_url: 'census/transaction/del',
                    multi_url: 'census/transaction/multi',
                    import_url: 'census/transaction/import',
                    table: 'wanlshop_shop',
                    dragsort_url:'',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                fixedColumns: true,
                fixedRightNumber: 1,
                searchFormVisible:true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('门店'),visible: false,addClass:'selectpage',extend:'data-source="census/shop" data-primary-key="id" data-show-field="shopname"'},
                        {field: 'order.adatetime', title: __('日期'), operate:'RANGE',datetimeFormat: "YYYY-MM-DD", addclass:'datetimerange',defaultValue: this.today(), formatter: Table.api.formatter.datetime},
                        // {field: 'shopname', title: __('门店'),visible: false,operate: 'LIKE'},
                        {field: 'shopname', title: __('门店'),operate:false},
                        {field: 'commodity_order_amount', title: __('商品订单金额'),operate:false},
                        {field: 'service_order_amount', title: __('服务订单金额'),operate:false},
                        {field: 'carditem_order_amount', title: __('卡项订单金额'),operate:false},
                        {field: 'commodity_order_count', title: __('商品订单数'),operate:false},
                        {field: 'service_order_count', title: __('服务订单数'),operate:false},
                        {field: 'card_order_count', title: __('卡项订单数'),operate:false},
                        {field: 'total_order_fee', title: __('订单总额'),operate:false},
                        {field: 'total_order_count', title: __('订单总数'),operate:false},
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                        {
                            field: 'operate', 
                            title: __('Operate'), 
                            table: table, 
                            events: Table.api.events.operate,
                            buttons: 
                                [{
                                    name: 'detail',
                                    title: __('明细'),
                                    text: __('明细'),
                                    classname: 'btn btn-xs btn-danger btn-dialog',
                                    extend: 'data-area=\'["100%","100%"]\'',
                                    icon: '',
                                    url:  function (row, index, fields) {
                                        // 动态生成跳转URL，并添加参数
                                        return 'census/transaction/detail?shop_id='+row.id+'&createtime=' + row.order.adatetime;
                                    }
                                }],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            $("button[type='submit']").css("background-color","red");
            $("button[type='submit']").css("border-color","red");

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        //定义方法返回今日开始结束日期
        today: function () {
            const dd = new Date();
            const y = dd.getFullYear();// 获取今日年份
            const m = String(dd.getMonth() + 1).padStart(2, '0'); // 获取今日月份
            const d = String(dd.getDate()).padStart(2, '0'); // 获取今日日期

            const startOfDay = `${y}-${m}-${d} 00:00:00`;
            const endOfDay = `${y}-${m}-${d} 23:59:59`;

            return startOfDay + ' - ' + endOfDay;
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        detail:function(){
            var shop_id = Config.shop_id;
            var createtime = Config.createtime;
            Table.api.init({
                extend: {
                    index_url: 'census/transaction/detail' + location.search,
                    table: 'wanlshop_order',
                    dragsort_url:'',
                }
            });
            var table1 = $("#table");
            console.log('url:'+$.fn.bootstrapTable.defaults.extend.index_url);

            // 初始化表格
            table1.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                searchFormVisible:true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('ID'), operate: false,visible:false},
                        {field: 'order_no', title: __('订单编号'), operate: 'LIKE'},
                        {field: 'product_name', title: __('商品信息'), operate: 'LIKE'},
                        {field: 'paymenttime', title: __('付款时间'), operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'goods_type', title: __('账单类型'), searchList: {"0":__('商品'),"1":__('服务'),"2":__('卡项')}, formatter: Table.api.formatter.normal,operate:false},
                        {field: 'amount', title: __('交易金额'),operate:false},
                        {field: 'name', title: __('支付方式'),formatter: Table.api.formatter.normal,operate:false},
                        {
                            field: 'operate', 
                            title: __('Operate'), 
                            table: table1, 
                            events: Table.api.events.operate,
                            buttons: 
                                [{
                                    name: 'relation',
                                    title: __('关联订单'),
                                    text: __('关联订单'),
                                    classname: 'btn btn-xs btn-danger btn-dialog',
                                    icon: '',
                                    extend: 'data-area=\'["100%","100%"]\'',
                                    url:  function (row, index, fields) {
                                        // 动态生成跳转URL，并添加参数
                                        return row.j_url;
                                    }
                                }],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            $("button[type='submit']").css("background-color","red");
            $("button[type='submit']").css("border-color","red");

            // 为表格绑定事件
            Table.api.bindevent(table1);
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
